package com.sihai.ehr.service.impl;

import com.sihai.core.DataAccessor;
import com.sihai.core.Limit;
import com.sihai.core.LimitedList;
import com.sihai.core.Sorter;
import com.sihai.ehr.model.biz.Message;
import com.sihai.ehr.model.sys.User;
import com.sihai.ehr.service.MessageService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * author: wxn
 * since: 13-5-2 下午9:54
 */
@Service("messageService")
public class MessageServiceImpl implements MessageService {
    @Resource
    private DataAccessor dataAccessor;

    @Override
    public void save(Message message) throws Exception {
        Assert.notNull(message, "message is required");
        dataAccessor.save(message);
    }

    @Override
    public Message get(Long id) throws Exception {
        Assert.notNull(id, "message id is required");
        return dataAccessor.get(Message.class, id);
    }

    @Override
    public boolean delete(String ids) throws Exception {
        return false;
    }

    @Override
    public LimitedList<Message> findByLoginUser(Long uid, Limit limit, Sorter sorter) throws Exception {
        Assert.notNull(uid, "user id is required");
        User user = dataAccessor.get(User.class, uid);
        Assert.notNull(user, "user  is required");
        return dataAccessor.findByHql(Message.class, "from Message m where m.work = ? or m.duty=?", new Object[]{user.getWork(), user.getWork().getDuty()}, limit, sorter);
    }
}
